package com.app.babygo.dbservice;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

public class MongoDBClient {
	
	private Mongo mongo;
    private DB db;
    
    public MongoDBClient(String host, String dbname) throws UnknownHostException {
        
    	mongo = new MongoClient(host, 27017);
        
        db = mongo.getDB(dbname);
    }
    
    public void savePhoto(String fileName, String filePath) throws IOException {
    	File imageFile = new File(filePath);
    	GridFS gfsPhoto = new GridFS(db, "babyPhotos");
    	GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);
    	gfsFile.setFilename(fileName);
    	gfsFile.setContentType("photo");
    	gfsFile.save();
    }
    
    public GridFSDBFile findPhoto(String fileName) {
    	GridFS gfsPhoto = new GridFS(db, "babyPhotos");
    	GridFSDBFile imageForOutput = gfsPhoto.findOne(fileName);
    	return imageForOutput;
    }
    
    //    public static void main(String[] args) {
    //    	try {
    //			MongoDBClient client = new MongoDBClient("localhost", "babyGoDB");
    //			client.savePhoto("images.jpeg", "/Users/kukabox/Downloads/images.jpeg");
    //		} catch (UnknownHostException e) {
    //			// TODO Auto-generated catch block
    //			e.printStackTrace();
    //		} catch (IOException e) {
    //			// TODO Auto-generated catch block
    //			e.printStackTrace();
    //		}
    //    }
}
